/**
 * 产品分类列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'zmnExportUtil', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var zmnExportUtil = layui.zmnExportUtil;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-149',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        },
    });

    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    var ids = new Array();

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        let items = obj.config.data;
        ids = zmnPage.layIdsChecked(items, "categId");

        //导出excel
        if(obj.event=='CUSTOM_EXPORT'){
            zmnExportUtil.doExportExcel('zmn-table','后台产品-产品一级分类');
            return;
        }

        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        if (ids.length === 0) {
            return layer.alert("请选择至少一个分类！");
        }
        zmnPage.layStartStop(obj, "/categ/updateStatus.action", ids.join(","));
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        if (obj.data.sort != null && obj.data.sort != ""){
            if (!obj.data.sort.match('^[1-9]\\d*$')){
                layer.alert("排序字段请输入正整数");
                return false;
            }

            if (obj.data.sort.length > 8){
                layer.alert("排序字段长度不能超过8位");
                return false;
            }
        }
        zmnPage.laySort(obj, "/categ/updateSort.action", obj.data.categId);
    });

    form.on("switch(chk_one_status)", function (obj) {
        let  url = "/categ/updateStatus.action";
        let categId = obj.elem.getAttribute("key");
        let status = obj.elem.checked ? 2 : 1;
        switch (status) {
            case 1:
                zmnPage.ajaxSimple(url, {ids: categId, status: 1}, 1000);
                break;
            case 2:
                zmnPage.ajaxSimple(url, {ids: categId, status: 2}, 1000);
                break;
        }
        setTimeout(function () {
            location.reload(true);
        }, 3000);
    });

    //对外暴露的接口
    exports('art_categ_list', {});
});

/**
 * 描述: 新增/修改产品分类名称
 **/
function modifyProductCategoryName(categId){
    if (categId == ""){
        zmnLayUtil.openIframeCustom(layer, "/categ/detail.action", {title: "新增产品分类", shade: 0.8, area:["550px", "270px"]});
    } else {
        zmnLayUtil.openIframeCustom(layer, "/categ/detail.action?categId="+categId, {title: "修改产品分类", shade: 0.8, area:["550px", "270px"]});
    }
}

//显示描述
function show_tips(flag, tips, obj){
    //小tips
    //如果没有权限则提示
    if (!flag) {
        layer.tips(tips,obj,{
            tips:[1,'#3595CC'],
            time:3000
        })
    }
}
